package cloud.seri.iot.manager.web.rest

import cloud.seri.iot.manager.service.CommodityService
import cloud.seri.iot.manager.web.rest.errors.BadRequestAlertException
import cloud.seri.iot.manager.service.dto.CommodityDTO

import io.github.jhipster.web.util.HeaderUtil
import io.github.jhipster.web.util.ResponseUtil
import io.swagger.annotations.ApiOperation
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Value
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.DeleteMapping
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.PutMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.RestController

import javax.validation.Valid
import java.net.URI
import java.net.URISyntaxException

/**
 * REST controller for managing [cloud.seri.iot.manager.domain.Commodity].
 */
@RestController
@RequestMapping("/api")
class CommodityResource(
    val commodityService: CommodityService
)
{

    private val log = LoggerFactory.getLogger(this.javaClass)

    @Value("\${jhipster.clientApp.name}")
    private var applicationName: String? = null

    /**
     * `POST  /commodities` : Create a new commodity.
     *
     * @param commodityDTO the commodityDTO to create.
     * @return the [ResponseEntity] with status `201 (Created)` and with body the new commodityDTO, or with status `400 (Bad Request)` if the commodity has already an ID.
     * @throws URISyntaxException if the Location URI syntax is incorrect.
     */
    @PostMapping("/commodities")
    @ApiOperation("新建商品", nickname = "CreateCommodity")
    fun createCommodity(@Valid @RequestBody commodityDTO: CommodityDTO): ResponseEntity<CommodityDTO>
    {
        log.debug("REST request to save Commodity : {}", commodityDTO)
        if (commodityDTO.id != null)
        {
            throw BadRequestAlertException("A new commodity cannot already have an ID", ENTITY_NAME, "idexists")
        }
        val result = commodityService.save(commodityDTO)
        return ResponseEntity.created(URI("/api/commodities/" + result.id))
            .headers(HeaderUtil.createEntityCreationAlert(applicationName, true, ENTITY_NAME, result.id.toString()))
            .body(result)
    }

    /**
     * `PUT  /commodities` : Updates an existing commodity.
     *
     * @param commodityDTO the commodityDTO to update.
     * @return the [ResponseEntity] with status `200 (OK)` and with body the updated commodityDTO,
     * or with status `400 (Bad Request)` if the commodityDTO is not valid,
     * or with status `500 (Internal Server Error)` if the commodityDTO couldn't be updated.
     * @throws URISyntaxException if the Location URI syntax is incorrect.
     */
    @PutMapping("/commodities")
    fun updateCommodity(@Valid @RequestBody commodityDTO: CommodityDTO): ResponseEntity<CommodityDTO>
    {
        log.debug("REST request to update Commodity : {}", commodityDTO)
        if (commodityDTO.id == null)
        {
            throw BadRequestAlertException("Invalid id", ENTITY_NAME, "idnull")
        }
        val result = commodityService.save(commodityDTO)
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert(applicationName, true, ENTITY_NAME, commodityDTO.id.toString()))
            .body(result)
    }

    /**
     * `GET  /commodities` : get all the commodities.
     *
     * @return the [ResponseEntity] with status `200 (OK)` and the list of commodities in body.
     */
    @GetMapping("/commodities")
    fun getAllCommodities(): MutableList<CommodityDTO>
    {
        log.debug("REST request to get all Commodities")
        return commodityService.findAll()
    }

    /**
     * `GET  /commodities/:id` : get the "id" commodity.
     *
     * @param id the id of the commodityDTO to retrieve.
     * @return the [ResponseEntity] with status `200 (OK)` and with body the commodityDTO, or with status `404 (Not Found)`.
     */
    @GetMapping("/commodities/{id}")
    fun getCommodity(@PathVariable id: String): ResponseEntity<CommodityDTO>
    {
        log.debug("REST request to get Commodity : {}", id)
        val commodityDTO = commodityService.findOne(id)
        return ResponseUtil.wrapOrNotFound(commodityDTO)
    }

    /**
     * `DELETE  /commodities/:id` : delete the "id" commodity.
     *
     * @param id the id of the commodityDTO to delete.
     * @return the [ResponseEntity] with status `204 (NO_CONTENT)`.
     */
    @DeleteMapping("/commodities/{id}")
    fun deleteCommodity(@PathVariable id: String): ResponseEntity<Void>
    {
        log.debug("REST request to delete Commodity : {}", id)
        commodityService.delete(id)
        return ResponseEntity.noContent().headers(HeaderUtil.createEntityDeletionAlert(applicationName, true, ENTITY_NAME, id)).build()
    }

    companion object
    {
        private const val ENTITY_NAME = "seriIotManagerCommodity"
    }
}
